package cn.wit.summit.chwan.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import cn.wit.summit.chwan.R;

/**
 * Created by yangg on 2018/4/12.
 */

public class ForumUtil {


    public static void goWelfareVip(View... views) {
        if (views == null) return;
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goSpec1(v.getContext());
            }
        };
        for (View view :
                views) {
            if (view != null) view.setOnClickListener(listener);
        }
    }
    public static void goSpec1(Context ctx) {
        goLinkInner(ctx, "http://anv3cjapi.papa91.com/member/vip_view/welfare/vip", null);
    }
    public static void goLinkInner(Context ctx, String linkValue, String title) {
//        IntentDateBean intentDateBean = new IntentDateBean();
//        intentDateBean.setLink_type(StaticFinalNumberUtil.LINK_TYPE_APP_INNER);
//        if (StringUtils.isNotEmpty(title))
//            intentDateBean.setObject(title);
//        intentDateBean.setLink_type_val(linkValue);
//        IntentUtil.getInstance().intentActivity(ctx, intentDateBean);
    }

    public static void setCommentReplyVipText(TextView textView, String from, int fromVip, int fromSVip
            , String to, int toVip, int toSVip, String msg) {
        Context context = textView.getContext();
        textView.setGravity(Gravity.CENTER_VERTICAL);
        String str          = "";
        int    startIndex[] = new int[1];
        int    endIndex[]   = new int[1];
        if (StringUtils.isNotEmpty(to)) {
            startIndex = new int[2];
            endIndex = new int[2];
            str = context.getString(R.string.comment_item_content_2, from, to, msg);

            startIndex[0] = str.indexOf(from);
            endIndex[0] = startIndex[0] + from.length();

            String strNonFrom = str.substring(endIndex[0]);
            startIndex[1] = endIndex[0] + strNonFrom.indexOf(to);
            endIndex[1] = startIndex[1] + to.length();
        } else {
            str = context.getString(R.string.comment_item_content_1, from, msg);
            startIndex[0] = str.indexOf(from);
            endIndex[0] = startIndex[0] + from.length();
        }
        SpannableString ss = new SpannableString(str);

        for (int i = 0; i < startIndex.length; i++) {
            int color = Color.parseColor("#2a72cb");
//            if (i==0) {
//                if (fromVip>0)
//                    color = Color.parseColor("#ff0000");
//                if (fromSVip>0)
//                    color = Color.parseColor("#f6c004");
//            } else {
//                if (toVip>0)
//                    color = Color.parseColor("#ff0000");
//                if (toSVip>0)
//                    color = Color.parseColor("#f6c004");
//            }
            ss.setSpan(new ForegroundColorSpan(color), startIndex[i], endIndex[i], Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (str.contains("#*#")) {
            Bitmap b       = BitmapFactory.decodeResource(textView.getContext().getResources(), R.mipmap.official_icon);
            ImageSpan imgSpan = new ImageSpan(textView.getContext(), b);
            String[]  strings = str.split("#\\*#");
            if (strings.length > 1) {
                ss.setSpan(imgSpan, strings[0].length(), strings[0].length() + 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                if (strings.length > 2) {
                    Bitmap    b2       = BitmapFactory.decodeResource(textView.getContext().getResources(), R.mipmap.official_icon);
                    ImageSpan imgSpan2 = new ImageSpan(textView.getContext(), b2);
                    ss.setSpan(imgSpan2, str.indexOf(strings[1]) + strings[1].length(), str.indexOf(strings[1]) + strings[1].length() + 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }

        }
        textView.setText(ss);
    }
}
